<?php

namespace App\Models;

use App\Models\Traits\BaseModels;
use Illuminate\Database\Eloquent\Model;

/**
 * Class UserInfo
 * Description:用户基础信息模型
 * Author: Sam
 * DateTime: 2019/1/2 10:24
 *
 * @package App\Models
 * @property int $id
 * @property int $user_id 用户id
 * @property string $education 学历
 * @property string $company 工作单位
 * @property string $work_address 工作地址
 * @property string $work_phone 工作单位电话
 * @property string $linkman_first 紧急联系人1
 * @property string $linkman_first_relation 紧急联系人1关系
 * @property string $linkman_first_phone 紧急联系人1电话
 * @property string $linkman_second 紧急联系人2
 * @property string $linkman_second_relation 紧急联系人2关系
 * @property string $linkman_second_phone 紧急联系人2电话
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereEducation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereLinkmanFirst($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereLinkmanFirstPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereLinkmanFirstRelation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereLinkmanSecond($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereLinkmanSecondPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereLinkmanSecondRelation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereWorkAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereWorkPhone($value)
 * @mixin \Eloquent
 * @property string $phone_address_list 通讯录
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo wherePhoneAddressList($value)
 * @property-read mixed $edu_str
 * @property-read array|mixed $phone_list
 * @property string $profession 职业
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserInfo whereProfession($value)
 */
class UserInfo extends Model
{
    use BaseModels;
    /**
     * 硕士
     */
    CONST EDU_MASTER = '01';

    /**
     * 本科
     */
    CONST EDU_UNDERGRADUATE = '02';

    /**
     * 大专
     */
    CONST EDU_COLLEGE = '03';

    /**
     * 高中
     */
    CONST EDU_HIGH = '04';

    /**
     * 初中
     */
    CONST EDU_JUNIOR = '05';

    /**
     * 未知
     */
    CONST EDU_UNKNOWN = '99';

    /**
     * 配偶
     */
    CONST RELATION_SPOUSE = '1';

    /**
     * 父母
     */
    CONST RELATION_PARENTS = '2';

    /**
     * 子女
     */
    CONST RELATION_CHILDREN = '3';

    /**
     * 兄弟姐妹
     */
    CONST RELATION_BRO_SIS = '4';

    /**
     * 朋友
     */
    CONST RELATION_FRIEND = '5';

    /**
     * 同事
     */
    CONST RELATION_WORKMATE = '6';

    /**
     * 其他
     */
    CONST RELATION_OTHER = '7';

    /*
     * 新流调用流程标志
     * */
    CONST FLOW_FLAG_FULL = 1, FLOW_FLAG_TMP = 0;

    /**
     * 枚举学历数据
     * */
    static $EDUCATION_ENUM = array(
        self::EDU_MASTER        => '硕士',
        self::EDU_UNDERGRADUATE => '本科',
        self::EDU_COLLEGE       => '大专',
        self::EDU_HIGH          => '高中',
        self::EDU_JUNIOR        => '初中',
        self::EDU_UNKNOWN       => '未知'
    );

    /*
     * 枚举关系数据
     * */
    static $USER_RELATION = array(
        self::RELATION_SPOUSE   => '配偶',
        self::RELATION_PARENTS  => '父母',
        self::RELATION_CHILDREN => '子女',
        self::RELATION_BRO_SIS  => '兄弟姐妹',
        self::RELATION_FRIEND   => '朋友',
        self::RELATION_WORKMATE => '同事',
        self::RELATION_OTHER    => '其他'
    );

    protected $guarded = ['id'];

    protected $appends = ['phone_list','edu_str'];

    /**
     * 数组格式返回通讯录
     * @return array|mixed
     * @author luwei
     * @date 2019-01-06 14:14
     */
    public function getPhoneListAttribute(){
        if($this->attributes['phone_address_list']){
            return json_decode($this->attributes['phone_address_list'], true);
        }
        return [];
    }

    /**
     * 获取学历信息
     * @return mixed
     * @author luwei
     * @date 2019-01-06 14:22
     */
    public function getEduStrAttribute(){
        $eduArray = array_keys(self::$EDUCATION_ENUM);
        if($this->attributes['education'] && in_array($this->attributes['education'],$eduArray)){
            return self::$EDUCATION_ENUM[$this->attributes['education']];
        }
        return self::$EDUCATION_ENUM[self::EDU_UNKNOWN];
    }

    /**
     * json格式存数据库
     * @param $val
     * @author luwei
     * @date 2019-01-10 16:20
     */
    public function setPhoneAddressListAttribute($val){
        $this->attributes['phone_address_list'] = json_encode($val);
    }
}
